package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import sanrenxing.dayEnd.backRepertoryTotal.domain.ReceivableAndPayable;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desrciption 证券应收应付 dao层
 * @auth：yidu 李小锋
 * @date:2021/3/14 0014 - 9:23
 * @version:1:0
 */
@Repository
public interface SecuritiesDao {
    /**
     * 将证券应收应付信息添加到数据库表中
     * @param srap 证券应收应付对象
     * @return 1：添加成功；0：添加失败
     */
    @Insert("insert into receivableAndPayable values(seqReceivableAndPayable.nextval,#{businDate},#{caId},#{seId},#{businType},#{capflow},#{money},#{fundId},#{states})")
    public int add(ReceivableAndPayable srap);
    /**
     * 将证券应收应付信息添加到数据库表中
     * @param srap 证券应收应付对象
     * @return 1：修改成功；0：修改失败
     */
    @Update("update receivableAndPayable set " +
            "businDate=#{businDate},caId=#{caId},seId=#{seId},businType=#{businType}," +
            "capflow=#{capflow},money=#{money},fundId=#{fundId},states=#{states} where srapId=#{srapId}")
    public int update(ReceivableAndPayable srap);
    /**
     * 根据证券应收应付编号删除证券应收应付信息
     * @param srapId 证券应收应付编号
     * @return 1：删除成功；0：删除失败
     */
    @Delete("delete from receivableAndPayable where srapId=#{srapId}")
    public int delete(@Param("srapId") String srapId);

    /**
     * 表示权益中的删除
     * @param seId
     * @param businDate
     * @return
     */
    @Delete("delete from receivableAndPayable where seId=#{seId} and businDate=#{businDate}")
    public int  EquityDateDelete(@Param("seId") String seId, @Param("businDate") Date businDate);

    /**
     * 表示证券交易数据中的删除
     * @param ceId
     * @param tranDate
     * @return
     */
    @Delete("delete from receivableAndPayable where caId=#{ceId} and businDate=#{tranDate}")
    public int  TransactionDelete(@Param("ceId") String ceId, @Param("tranDate") Date tranDate);
    /**
     * 查询所有的订单数据
     * @return 返回一个订单集合
     */
    @Select("select * from receivableAndPayable")
    @Results(id = "securMap",value = {
            @Result(id = true,column = "srapId",property = "srapId"),
            @Result(column = "businDate",property = "businDate"),
            @Result(column = "caId",property = "caId"),
            @Result(column = "seId",property = "seId"),
            @Result(column = "businType",property = "businType"),
            @Result(column = "capflow",property = "capflow"),
            @Result(column = "money",property = "money"),
            @Result(column = "fundId",property = "fundId")
    })
    public List<ReceivableAndPayable> findAll();
    /**
     * 分页查询
     * @param start  开始行号
     * @param rows   每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @ResultMap(value = "securMap")
    @Select("<script>"+
            "select * from " +
            "(select rownum  rn,receivableAndPayable.* from receivableAndPayable " +
            "<where> #{rows}>rownum </where>)"+
            "<where>"+
            /*"<if test='rn!=0'>"+*/
            " ${condition}"+
            /*"</if>"+*/
            " and rn>=#{start}"+
            "</where>"+
            "</script>")
    public List<ReceivableAndPayable> findByPage(@Param("start") int start, @Param("rows") int rows, @Param("condition") String condition);
    /**
     * 按条件统计出记录行数
     * @param condition 条件
     * @return 记录行数
     */
    @Select("select count(*) from ReceivableAndPayable where 1=1 ${condition}")
    public int count(@Param("condition") String condition);

    /**
     * 取出应冲销的证券应收应付的数据id
     * @return
     */
    @Select("select ra.srapId,ra.srapIds from (select rap.srapId as srapId,raps.srapId as srapIds,rownum rn from receivableAndPayable rap join receivableAndPayable raps on rap.businType=raps.businType " +
            "where rap.srapId<>raps.srapId and rap.money=raps.money and rap.capflow<>raps.capflow and rap.seId<>'无' and raps.seId='无') ra where ra.rn between 0 and 1")
    public Map<String,String> washDelete();
}
